package com.mundus.engine.assets.vertex;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.util.Color;
import org.lwjgl.util.vector.Vector3f;

public class VertexPosCol implements Vertex{
	private static final int POSITIONELEMENTCOUNT = 4;
	private static final int COLORELEMENTCOUNT = 4;
	
	private static int POSITIONBYTESCOUNT = POSITIONELEMENTCOUNT * ELEMENTBYTES;
	private static int COLORBYTESCOUNT = COLORELEMENTCOUNT * ELEMENTBYTES;

	private static int POSITIONBYTEOFFSET = 0;
	private static int COLORBYTEOFFSET = POSITIONBYTEOFFSET + POSITIONBYTESCOUNT;
	
	private static int ELEMENTCOUNT = POSITIONELEMENTCOUNT + COLORELEMENTCOUNT;
	private static int SIZEINBYTES = POSITIONBYTESCOUNT + COLORBYTESCOUNT;
	
	private Vector3f _position;
	private Color _color;
	
	public Vector3f getPositionv() { return _position;}
	public Color getColor() { return _color; }
	
	public float[] getPositionf() { return new float[]{ _position.x, _position.y, _position.z};}
	public float[] getColorf() {
		return new float[]{ _color.getRed()/255,
			_color.getGreen()/255,
			_color.getBlue()/255,
			_color.getAlpha()/255};
	}

	public void setPosition(Vector3f position) { this._position = position; }
	public void setColor(Color color) { this._color = color; }
	
	public VertexPosCol(Vector3f position, Color color){
		this._position = position;
		this._color = color;
	}
	
	public float[] getElements(int elementCount){
		float[] elements = new float[elementCount];
		
		int i=0;
		//POSITION
		elements[i++] = this._position.x;
		elements[i++] = this._position.y;
		elements[i++] = this._position.z;
		elements[i++] = 1;
		//COLOR
		elements[i++] = this._color.getRed()/255;
		elements[i++] = this._color.getGreen()/255;
		elements[i++] = this._color.getBlue()/255;
		elements[i++] = this._color.getAlpha()/255;
		
		return elements;
	}
	
	public int getVAO(Vertex[] vertices){
		FloatBuffer vBuffer = BufferUtils.createFloatBuffer(vertices.length * ELEMENTCOUNT);
		for(Vertex v : vertices){
			VertexPosCol vPC = (VertexPosCol)v;
			vBuffer.put(vPC.getElements(ELEMENTCOUNT));
		}
		vBuffer.flip();			
		
		int vaoId = GL30.glGenVertexArrays();
		GL30.glBindVertexArray(vaoId);

		int vboId = GL15.glGenBuffers();
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
		GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vBuffer, GL15.GL_STATIC_DRAW);
		GL20.glVertexAttribPointer(0, POSITIONELEMENTCOUNT, GL11.GL_FLOAT, false, SIZEINBYTES, POSITIONBYTEOFFSET);
		GL20.glVertexAttribPointer(1, COLORELEMENTCOUNT, GL11.GL_FLOAT, false, SIZEINBYTES, COLORBYTEOFFSET);
		GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

		GL30.glBindVertexArray(0);
		
		return vaoId;
	}
	
	public VertexType getVertexType(){
		return VertexType.VertexPosCol;
	}
	
	public void setActiveAttributes() {
		GL20.glEnableVertexAttribArray(0);
		GL20.glEnableVertexAttribArray(1);
	}
}
